package com.mobimagic.lockscreen.view;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mobimagic.lockscreen.R;
import com.mobimagic.lockscreen.activity.ChargingProgressActivity;
import com.mobimagic.lockscreen.font.TypeFaceManager;
import com.mobimagic.lockscreen.sdk.LockScreenSDK;
import com.mobimagic.lockscreen.util.ScreenUtil;

public class DateView extends LinearLayout implements View.OnClickListener{
    public DateView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private TextView hourMinuteTextView, dayWeekMonthTextView;
    private String dayOfHourMin;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        hourMinuteTextView = (TextView) findViewById(R.id.hour_minute);
        dayWeekMonthTextView = (TextView) findViewById(R.id.day_in_weak);
        Typeface fontFace = TypeFaceManager.getInstance().getTypeface(TypeFaceManager.FONT_ROBOTO_THIN, Typeface.NORMAL);
        hourMinuteTextView.setTypeface(fontFace);
        updateCurrentDate();
        float dy = ScreenUtil.dpToPx(getContext(), 2);
        hourMinuteTextView.setShadowLayer(dy, 0, dy, 0x26000000);
        dayWeekMonthTextView.setShadowLayer(dy, 0, dy, 0x26000000);
        if (MobileChargingView.TEST_MODE) {
            setOnClickListener(this);
        }
    }

    /**
     * 刷新当前时间
     */
    public void updateCurrentDate() {
        Calendar calendar = Calendar.getInstance();
        boolean is24Hour = DateFormat.is24HourFormat(getContext());
        if (is24Hour) {
            dayOfHourMin = getResources().getString(R.string.date_hour_minute);
        } else {
            dayOfHourMin = getResources().getString(R.string.date_hour_minute_12);
        }
        String monthDay = getResources().getString(R.string.date_month_day);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        Date currentDate = new Date(calendar.getTimeInMillis());
        String hourAndMin = new SimpleDateFormat(dayOfHourMin).format(currentDate);
        String monthAndDay = new SimpleDateFormat(monthDay).format(currentDate);
        try {
            hourMinuteTextView.setText(hourAndMin);
            String weeks[] = LockScreenSDK.getInstance().getStringArray(R.array.week);
            String week = weeks[dayOfWeek - 1];
            String data = String.format("%1$s,%2$s", week, monthAndDay);
            dayWeekMonthTextView.setText(data);
        } catch (Exception e) {
        }
    }

    @Override
    public void onClick(View v) {
        if (MobileChargingView.TEST_MODE) {
            Intent intent = new Intent(getContext(), ChargingProgressActivity.class);
            getContext().startActivity(intent);
        }
    }
}
